<template>
  <div class="mod-role">
    <el-form
      :inline="true"
              size="small"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item label="角色名称">
        <el-input
          v-model="dataForm.roleName"
          placeholder="角色名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="角色类型">
        <el-select v-model="dataForm.type" clearable placeholder="请选择">
          <el-option
            v-for="item in roleTypeArray"
            :key="item.value"
            :label="item.text"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色标识">
        <el-input
          v-model="dataForm.roleSign"
          placeholder="角色标识"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>

        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
        <!-- <el-button v-if="isAuth('sys:role:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <!-- <el-button v-if="isAuth('sys:role:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
       size="mini"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column type="index" align="center" label="编号" width="100" />
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="角色名称"
      >
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="角色类型"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 1" size="mini">系统角色</el-tag>
          <el-tag v-else-if="scope.row.type === 2" size="mini" type="success"
            >自定义角色</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="sign"
        header-align="center"
        align="center"
        label="角色标识"
      >
      </el-table-column>
      <el-table-column
        prop="dataState"
        header-align="center"
        align="center"
        width="80"
        label="角色状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState === 0" size="small">启用</el-tag>
          <el-tag
            v-else-if="scope.row.dataState === -1"
            size="small"
            type="danger"
            >冻结</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <!-- <el-button v-if="isAuth('sys:role:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          <!-- <el-button v-if="isAuth('sys:role:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button> -->
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.dataState === -1"
            type="text"
            size="small"
            @click="enableHandle(scope.row.id)"
            >启用</el-button
          >
          <el-button
            v-if="scope.row.dataState === 0"
            type="text"
            size="small"
            @click="disableHandle(scope.row.id)"
            >冻结</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./role-add-or-update";
import { pageList, enable, disable, remove } from "@/api/role";

export default {
  data() {
    return {
      roleTypeArray: [
        { value: 1, text: "固定角色" },
        { value: 2, text: "自定义角色" },
      ],
      dataForm: {
        roleName: "",
        type: "",
        roleSign: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      pageList({
        name: this.dataForm.roleName,
        type: this.dataForm.type,
        sign: this.dataForm.roleSign,
        pageNumber: this.pageIndex,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res && res.code === 0) {
          var data = res.data.data;
          this.dataList = data;
          this.totalPage = data.count;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    dataStateChange(chooseState) {
      if (chooseState) {
        this.enableHandle();
      } else {
        this.disableHandle();
      }
      console.log(chooseState);
      return;
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enable({ id: id }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    enableHandle(id) {
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enable({ id: id }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    disableHandle(id) {
      this.$confirm(`确定进行冻结操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          disable({ id: id }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });

      this.$confirm(`是否确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          remove({ ids: ids }).then((data) => {
            debugger;
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
